GOSU_DIR = @abs_top_srcdir@/..

BASE_OBJS = Math.o Utility.o IO.o FileUnix.o TimingUnix.o WindowX.o	\
	    Graphics/Bitmap.o InputX.o TextInputX.o			\
	    Graphics/BitmapUtils.o Graphics/Color.o			\
	    Graphics/TexChunk.o Graphics/Graphics.o Graphics/Image.o	\
	    Graphics/BlockAllocator.o		\
	    Graphics/Texture.o Graphics/LargeImageData.o		\
	    Graphics/BitmapFreeImage.o Graphics/Font.o \
	    Graphics/TextUnix.o Graphics/Text.o	Graphics/Transform.o		\
	    Graphics/BitmapColorKey.o DirectoriesUnix.o Audio/AudioOpenAL.o \
	    Inspection.o

NET_OBJS = Sockets/CommSocket.o Sockets/ListenerSocket.o	\
	   Sockets/Socket.o Sockets/MessageSocket.o

OBJS = @GOSU_A_OBJS@

XPLS = $(GOSU_DIR)/examples/Tutorial $(GOSU_DIR)/examples/TextInput

ALL_CXXFLAGS = -fPIC -I.. -Wall @PANGO_CXX_FLAGS@ @X_INCS@ \
	   @HAVE_SYS_STAT_H@ \
	   -DNDEBUG -O3 -pipe \
	   -I/usr/include/SDL $(CXXFLAGS)

LIBS = -L@x_libraries@ -lX11 @PANGO_LIBS@ -lsndfile -lvorbisfile -lvorbis -logg \
       -lm -lopenal -lpng -lstdc++ -lGL -lSDL_ttf -lfreeimage

LDCONFIG = ldconfig
INSTALL = install
MKDIR = mkdir -p
TRY_RMDIR = rmdir --ignore-fail-on-non-empty

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
includedir=@includedir@
libdir=@libdir@

INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644

.SUFFIXES: .cpp .o

all: directories ../lib/libgosu.a ../lib/libgosu.so ../bin/gosu-config

directories:
	$(MKDIR) ../bin ../lib Graphics Sockets Audio

../lib/libgosu.a: $(OBJS)
	$(AR) -r ../lib/libgosu.a $(OBJS)

../lib/libgosu.so: $(OBJS)
	$(CXX) -shared -o ../lib/libgosu.so ${LIBS} $(OBJS)

install: all
	$(MKDIR) $(bindir) $(libdir) $(includedir)/Gosu
	$(INSTALL_DATA) ../lib/libgosu.a $(libdir)
	$(INSTALL_DATA) ../lib/libgosu.so $(libdir)
	$(INSTALL_PROGRAM) ../bin/gosu-config $(bindir)
	$(INSTALL_DATA) ../Gosu/* $(includedir)/Gosu
	$(LDCONFIG) -n $(libdir)

uninstall:
	rm -f $(libdir)/libgosu.a
	rm -f $(libdir)/libgosu.so
	rm -f $(bindir)/gosu-config
	rm -rf $(includedir)/Gosu
	$(LDCONFIG) -n $(libdir)
	$(TRY_RMDIR) $(includedir) $(libdir) $(bindir) $(prefix)

clean:
	rm -f $(OBJS) $(XPLS) ../lib/libgosu* ../lib/gosu.for* ../bin/gosu-config
	rm -rf Audio Graphics Sockets

$(OBJS): %.o: $(GOSU_DIR)/GosuImpl/%.cpp
	$(CXX) $(GOSU_DIR)/GosuImpl/$*.cpp -o $@ $(ALL_CXXFLAGS) -c

examples: $(XPLS) ../lib/libgosu.a

$(XPLS): %: %.cpp ../lib/libgosu.a
	$(CXX) $@.cpp -o $@ $(ALL_CXXFLAGS) $(LIBS) ../lib/libgosu.a

../bin/gosu-config:
	echo "#!/bin/bash" > ../bin/gosu-config
	echo "while test \$$# -gt 0; do" >> ../bin/gosu-config
	echo "    case \$$1 in" >> ../bin/gosu-config
	echo "    --libs)" >> ../bin/gosu-config
	echo '    echo "$(LIBS)";;' >> ../bin/gosu-config
	echo "    --cxxflags)" >> ../bin/gosu-config
	echo '    echo "$(ALL_CXXFLAGS)";;' >> ../bin/gosu-config
	echo "    esac" >> ../bin/gosu-config
	echo "    shift" >> ../bin/gosu-config
	echo "done" >> ../bin/gosu-config
	chmod u+x ../bin/gosu-config
